package com.demo.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.demo.entity.Vo.DepartmentVo;
import com.demo.entity.Vo.PatientIPCVO;
import com.demo.entity.Vo.VtePreventiveDoctorAdviceVo;
import com.demo.entity.VtePreventiveDoctorAdvice;
import com.demo.websocket.vo.IpcDoctorAdvice;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

import java.util.Date;
import java.util.List;

/**
 * vte预防处置医嘱 Mapper 接口
 *
 * @author zm
 * @since 2020-11-27
 */
public interface VtePreventiveDoctorAdviceMapper extends BaseMapper<VtePreventiveDoctorAdvice> {

    /**
     * 获取医嘱列表
     *
     * @param pageNmber 页码
     * @param pageSize  大小
     * @param patientId 患者ID
     * @return
     */
    List<VtePreventiveDoctorAdviceVo> getDoctorAdvice(@Param("pageNmber") Integer pageNmber, @Param("pageSize") Integer pageSize, @Param("patientId") Integer patientId
            , @Param("adviseType") Integer adviseType, @Param("status") Integer status, @Param("validityLength") Integer validityLength);

    /**
     * 查询患者是否存在重复医嘱---药物
     *
     * @param patientId
     * @return
     */
    List<Integer> selectRepeatDoctor(@Param("patientId") Integer patientId, @Param("medicineId") Integer medicineId);

    /**
     * 查询患者是否存在重复医嘱---物理
     *
     * @param patientId
     * @return
     */
    List<Integer> getConflictDoctorPhysical(@Param("patientId") Integer patientId);

    VtePreventiveDoctorAdvice getAdviseCoten(@Param("patientId") Integer patientId, @Param("medicineId") Integer medicineId);

    /**
     * 根据病区编号获取IPC医嘱列表
     *
     * @param wardId     页码
     * @param pageNumber 页码
     * @param pageSize   大小
     * @param wardId     病区ID
     * @return
     */
    List<PatientIPCVO> getDoctorIpcAdviceByWardId(@Param("wardId") String wardId, @Param("pageSize") Integer pageNumber, @Param("offsetNumber") Integer pageSize);

    Integer getDoctorAdviceSumByDeptSn(@Param("deptSn") Integer paramInteger1, @Param("adviseType") Integer paramInteger2, @Param("type") String paramString);

    Integer getDoctorAdviceDurationSumByDeptSn(@Param("departments") List<Integer> paramList, @Param("startDate") Date paramDate1, @Param("endDate") Date paramDate2);

    List<DepartmentVo> getVteAdviceDeptSnRanking(@Param("orderType") String paramString, @Param("topNumber") Integer paramInteger);

    @Select("select * from pd_vte_preventive_doctor_advice where advise_type = 1 and status = 1 and deleted_time is null and drug_id=#{drugId}")
    List<VtePreventiveDoctorAdvice> queryPatientsByDrugId(Integer drugId);

    /**
     * 用于出院
     *
     * @param status
     * @param patientId
     * @return
     */
    @Update("update pd_vte_preventive_doctor_advice set status=#{status}, updated_time=now(), updated_user_id=0 where patient_id=#{patientId}")
    Integer updateStatusByPatientId(@Param("status") int status, @Param("patientId") Integer patientId);

    /**
     * 更新停止时间为 null 的医嘱
     * @param patientId
     * @return
     */
    @Update("update pd_vte_preventive_doctor_advice set stoped_user_time=now() where patient_id=#{patientId} and stoped_user_time is null and deleted_time is null")
    Integer updateStopTimeByPatientId(Integer patientId);

    Integer selectByPatients(@Param("patientIds") List<Integer> patientIds);

    List<IpcDoctorAdvice> selectAdviceByPatients(@Param("patientIds") List<Integer> patientIds, @Param("pageSize") Integer pageSize, @Param("offsetNumber") Integer offsetNumber);

    /**
     * 将患者所有未停止的医嘱停止
     * @param patientId
     * @return
     */
    @Update("update pd_vte_preventive_doctor_advice set stoped_user_time=now(), status=2 where patient_id=#{patientId} and status=1")
    Integer stopUnStopAdvice(Integer patientId);

}
